<?php

namespace App\Http\Middleware;

use Closure;
use DB;

class Authenticate
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @param  string|null  $guard
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $token = $request->header('Authorization');
        // 用户验证信息
        $user = DB::table('users')
        ->where('token', $token)->first(); 
        if($user) {
            $request->user = $user;
            return $next($request);
        }

        return response([
            'data' => '', 'message' => 'Unauthorized.', 'status_code' => 401
        ], 401);
    }


}
